package com.gitee.ice1938.module.admin.app.manage.appdetail;

import java.security.Principal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.datatables.ajax.FormData;
import net.datatables.ajax.PageImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Order;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.gitee.ice1938.mvc.base.AjaxResult;
import com.gitee.ice1938.mvc.base.PageConvert;

/**
 * AppDetailController
 * 
 * 
 * @author ice1938@qq.com 2019-12-2 10:38:57
 *
 */
@RestController
@RequestMapping("/appdetail")
public class AppDetailRestController {

	@Autowired
	private AppDetailService appDetailService;

	/**
	 * 页面数据查询方法
	 * 
	 * @param request
	 *            请求
	 * @param response
	 *            响应
	 * @param model
	 *            Model
	 * @return 页面
	 */
	@RequestMapping(path = "/list", method = RequestMethod.POST)
	@ResponseBody
	public PageImpl<AppDetailEntity> list(HttpServletRequest request, HttpServletResponse response, Model model) {
		FormData formData = new FormData(request);
		Pageable pagebale = PageConvert.convertPageable(formData);
		Order order = PageConvert.convertOrder(formData);
		Page<AppDetailEntity> data = appDetailService.findAppDetailData(formData.getParam(), pagebale, order);
		return PageConvert.convertPage(data);
	}

	/**
	 * 插入数据方法
	 * 
	 * @param body
	 *            页面json 数据
	 * @return json 数据
	 */
	@RequestMapping(path = "/insert", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult insert(@RequestBody AppDetailEntity body, Principal principal) {
		String userLoginName = "anonymous";
		if (principal != null) {
			userLoginName = principal.getName();
		}
		int flag = appDetailService.insertAppDetail(body, userLoginName);
		AjaxResult result = new AjaxResult("添加成功!", 200);
		if (flag == 0) {
			result = new AjaxResult("未添加数据!", 200);
		}
		result.setData(body);
		return result;
	}

	/**
	 * 插入数据方法
	 * 
	 * @param body
	 *            页面json 数据
	 * @return json 数据
	 */
	@RequestMapping(path = "/update", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult update(@RequestBody AppDetailEntity body, Principal principal) {
		String userLoginName = "anonymous";
		if (principal != null) {
			userLoginName = principal.getName();
		}
		int flag = appDetailService.updateAppDetail(body, userLoginName);
		AjaxResult result = new AjaxResult("更新成功!", 200);
		if (flag == 0) {
			result = new AjaxResult("未更新数据!", 200);
		}
		result.setData(body);
		return result;
	}

	/**
	 * 删除数据方法
	 * 
	 * @param ids
	 *            页面json 数据
	 * @return json 数据
	 * @return
	 */
	@RequestMapping(path = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult delete(@RequestBody List<String> ids) {

		int flag = appDetailService.deleteAppDetailByIds(ids);
		AjaxResult result = new AjaxResult("删除成功!", 200);
		if (flag == 0) {
			result = new AjaxResult("未删除数据!", 200);
		}
		return result;
	}
}
